package cn.android.support.v7.lib.sin.crown.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 通过代码控制文本的圆角背景。
 * Created by 彭治铭 on 2018/1/18.
 */

public class ConerTextView extends android.support.v7.widget.AppCompatTextView {
    public ConerTextView(Context context) {
        super(context);
        init();
    }

    public ConerTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public Paint paint;

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.parseColor("#B4A4E0"));
        paint.setAntiAlias(true);
        paint.setDither(true);
        setLayerType(View.LAYER_TYPE_HARDWARE, paint);
    }

    int radius = ProportionUtils.getInstance().adapterInt(10);//圆角半径

    //注意，不能设置 android:gravity="center"。不然没有效果。
    @Override
    public void draw(Canvas canvas) {
        Path path = new Path();
        RectF rectF = new RectF(0, 0, getWidth(), getHeight());
        path.addRoundRect(rectF, radius, radius, Path.Direction.CW);
        canvas.drawPath(path, paint);
        super.draw(canvas);//必须放在最后，不如看不到。
    }
}
